﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;
    using WMS6.Web.UserControls;

    public class LotAttributeList : BasePage
    {
        protected Button btnAction;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F5072;
        protected GridView gvLotAttribute;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected Label lblLotAttribute;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2LotAttributeList(LotAttributeQueryEntity LotAttribute)
        {
            this.PagerFooter.CurrentPage = int.Parse(LotAttribute.CurrentPage);
            DataSet ds = new DataSet();
            ds = InventoryMgtServiceClientProxy.GetInstance().GetLotAttributeInfoByQueryEntity(LotAttribute);
            base.BindData2GridView(this.gvLotAttribute, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            LotAttributeQueryEntity lotAttributeQuery = this.GetLotAttributeQueryEntity();
            lotAttributeQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = lotAttributeQuery.CurrentPage;
            lotAttributeQuery.CurrentPage = "0";
            this.gvLotAttribute.Columns[1].Visible = false;
            this.gvLotAttribute.AllowSorting = false;
            this.BindData2LotAttributeList(lotAttributeQuery);
            this.gvLotAttribute.ShowFooter = false;
            lotAttributeQuery.IsGetAll = false;
            lotAttributeQuery.CurrentPage = currentPageIndex;
            this.SetLotAttributeEntity(lotAttributeQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("LotAttribute.xls", this.gvLotAttribute);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            LotAttributeQueryEntity lotAttributeQuery = this.GetLotAttributeQueryEntity();
            lotAttributeQuery.CurrentPage = "0";
            this.BindData2LotAttributeList(lotAttributeQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            LotAttributeQueryEntity lotAttributeQuery = this.GetLotAttributeQueryEntity();
            lotAttributeQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2LotAttributeList(lotAttributeQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            LotAttributeQueryEntity lotAttributeQuery = this.GetLotAttributeQueryEntity();
            lotAttributeQuery.OwnerID = "";
            lotAttributeQuery.SkuID = "";
            lotAttributeQuery.Lot = "";
            lotAttributeQuery.ExternalLot = "";
            lotAttributeQuery.ProduceDate = "";
            lotAttributeQuery.ExpiryDate = "";
            lotAttributeQuery.LotAttr01 = "";
            lotAttributeQuery.LotAttr02 = "";
            lotAttributeQuery.LotAttr03 = "";
            lotAttributeQuery.LotAttr04 = "";
            lotAttributeQuery.LotAttr05 = "";
            lotAttributeQuery.LotAttr06 = "";
            lotAttributeQuery.LotAttr07 = "";
            lotAttributeQuery.LotAttr08 = "";
            lotAttributeQuery.LotAttr09 = "";
            lotAttributeQuery.LotAttr10 = "";
            this.BindData2LotAttributeList(lotAttributeQuery);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("LotAttributeQuery.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            LotAttributeQueryEntity lotAttributeQuery = this.GetLotAttributeQueryEntity();
            if (int.Parse(lotAttributeQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                lotAttributeQuery.CurrentPage = (int.Parse(lotAttributeQuery.CurrentPage) + 1).ToString();
                this.BindData2LotAttributeList(lotAttributeQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            LotAttributeQueryEntity lotAttributeQuery = this.GetLotAttributeQueryEntity();
            if (int.Parse(lotAttributeQuery.CurrentPage) > 0)
            {
                lotAttributeQuery.CurrentPage = (int.Parse(lotAttributeQuery.CurrentPage) - 1).ToString();
                this.BindData2LotAttributeList(lotAttributeQuery);
            }
        }

        private LotAttributeQueryEntity GetLotAttributeQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as LotAttributeQueryEntity);
            }
            return (base.PageQueryEntitySession as LotAttributeQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                LotAttributeQueryEntity lotAttributeQuery = this.GetLotAttributeQueryEntity();
                lotAttributeQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2LotAttributeList(lotAttributeQuery);
            }
        }

        protected void gvLotAttribute_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5072", this.gvLotAttribute);
        }

        protected void gvLotAttribute_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvLotAttribute_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            this.hdnRtnValue.Value = base.UrlReplace(this.gvLotAttribute.DataKeys[e.NewSelectedIndex]["LOT"].ToString());
            if (!string.IsNullOrEmpty(this.hdnRtnValue.Value))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + this.hdnRtnValue.Value + "';  window.close();</script>", false);
            }
        }

        protected void gvLotAttribute_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                LotAttributeQueryEntity lotAttribute = this.GetLotAttributeQueryEntity();
                lotAttribute.SortField = e.SortExpression;
                if (lotAttribute.SortDirection == "ASC")
                {
                    lotAttribute.SortDirection = "DESC";
                }
                else if (lotAttribute.SortDirection == "DESC")
                {
                    lotAttribute.SortDirection = "ASC";
                }
                this.BindData2LotAttributeList(lotAttribute);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            LotAttributeQueryEntity lotAttributeQuery = new LotAttributeQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                lotAttributeQuery.OwnerID = this.PreviousPage.OwnerID;
                lotAttributeQuery.SkuID = this.PreviousPage.SkuID;
                lotAttributeQuery.Lot = this.PreviousPage.LotID;
                lotAttributeQuery.ExternalLot = this.PreviousPage.ExternalLotID;
                lotAttributeQuery.ProduceDate = this.PreviousPage.ProductDate;
                lotAttributeQuery.ExpiryDate = this.PreviousPage.ExpiryDate;
                lotAttributeQuery.LotAttr01 = this.PreviousPage.Attribute01;
                lotAttributeQuery.LotAttr02 = this.PreviousPage.Attribute02;
                lotAttributeQuery.LotAttr03 = this.PreviousPage.Attribute03;
                lotAttributeQuery.LotAttr04 = this.PreviousPage.Attribute04;
                lotAttributeQuery.LotAttr05 = this.PreviousPage.Attribute05;
                lotAttributeQuery.LotAttr06 = this.PreviousPage.Attribute06;
                lotAttributeQuery.LotAttr07 = this.PreviousPage.Attribute07;
                lotAttributeQuery.LotAttr08 = this.PreviousPage.Attribute08;
                lotAttributeQuery.LotAttr09 = this.PreviousPage.Attribute09;
                lotAttributeQuery.LotAttr10 = this.PreviousPage.Attribute10;
                lotAttributeQuery.VendorID = this.PreviousPage.VendorID;
                lotAttributeQuery.ReceiptDate = this.PreviousPage.ReceiptDate;
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            lotAttributeQuery.IsGetAll = false;
            lotAttributeQuery.CurrentPage = "0";
            lotAttributeQuery.PageSize = this.gvLotAttribute.PageSize.ToString();
            lotAttributeQuery.SortField = "L.ROWID";
            lotAttributeQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvLotAttribute.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvLotAttribute.Columns[1].Visible = false;
                this.gvLotAttribute.Columns[2].Visible = true;
                this.btnEdit.Visible = false;
                this.btnAction.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetLotAttributeEntity(lotAttributeQuery);
            this.BindData2LotAttributeList(lotAttributeQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetLotAttributeEntity(LotAttributeQueryEntity LotAttributeEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = LotAttributeEntity;
            }
            else
            {
                base.PageQueryEntitySession = LotAttributeEntity;
            }
        }

        public LotAttributeQuery PreviousPage
        {
            get
            {
                return (LotAttributeQuery) base.PreviousPage;
            }
        }
    }
}

